<?php
namespace common\models;

use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
use api\components\OperateException;
use Yii;

/**
 * 币记录数据模型
 * @author CGA
 */
class ARFreezeRecord extends ActiveRecord
{
    const TYPE_FREEZE = 1;
    const TYPE_UNFREEZE = 2;
    const TYPE_PAYOUT = 3;

    const ACCOUNT_TRADE = 1;
    const ACCOUNT_VOUCH = 2;

    const STATUS_UNDETERMINED = 1;
    const STATUS_WAITCONFIRM = 2;
    const STATUS_SUCCESS = 9;
    const STATUS_FAIL = -1;

    public static $categorys = [
        'trade_freeze'     => ['id' => 1, 'type' => self::TYPE_FREEZE, 'account' => self::ACCOUNT_TRADE], //交易冻结
        'trade_payout'     => ['id' => 2, 'type' => self::TYPE_PAYOUT, 'account' => self::ACCOUNT_TRADE], //交易支出
        'trade_revocation' => ['id' => 7, 'type' => self::TYPE_FREEZE, 'account' => self::ACCOUNT_TRADE], //撤销交易
        'trade_unfreeze'   => ['id' => 3, 'type' => self::TYPE_UNFREEZE, 'account' => self::ACCOUNT_TRADE], //撤销解冻
        'vouch_freeze'     => ['id' => 4, 'type' => self::TYPE_FREEZE, 'account' => self::ACCOUNT_VOUCH], //担保冻结
        'vouch_unfreeze'   => ['id' => 5, 'type' => self::TYPE_UNFREEZE, 'account' => self::ACCOUNT_VOUCH], //担保解冻
        'vouch_payout'     => ['id' => 6, 'type' => self::TYPE_PAYOUT, 'account' => self::ACCOUNT_VOUCH], //赔偿
    ];

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
        ];
    }

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%freeze_record}}';
    }

    /**
     * 获取用户信息
     */
    public function getUser()
    {
        return $this->hasOne(ARUser::className(), ['id' => 'uid']);
    }

    /**
     * 添加记录
     * @param nunber $uid        用户编号
     * @param nunber $coin       代币编号
     * @param nunber $amount     数额
     * @param nunber $amount_old 余额
     * @param string $operation  操作
     * @param nunber $serial_id  流水编号
     * @param nunber $opp_uid    对方用户编号
     */
    public static function addRecord($uid, $coin, $amount, $amount_old, $operation, $serial_id = null, $opp_uid = null)
    {
        if (!isset(self::$categorys[$operation])) {
            throw new OperateException('400005');
        } else {
            $operationInfo = self::$categorys[$operation];
            $model = new self();
            $model->type = $operationInfo['type'];
            $model->category = $operationInfo['id'];
            $model->uid = $uid;
            $model->account_type = $operationInfo['account'];
            if ($serial_id) {
                $model->serial_id = $serial_id;
            }
            if ($opp_uid) {
                $model->opp_uid = $opp_uid;
            }
            $scale = Yii::$app->params['scale'];
            $model->coin = $coin;
            $model->amount = $amount;
            $model->amount_old = sprintf("%.{$scale}f", $amount_old);
            if ($model->type === self::TYPE_FREEZE) {
                $model->amount_new = bcadd(sprintf("%.{$scale}f", $amount_old), sprintf("%.{$scale}f", $amount), $scale);
            } else {
                $model->amount_new = bcsub(sprintf("%.{$scale}f", $amount_old), sprintf("%.{$scale}f", $amount), $scale);
            }
            $model->status = self::STATUS_UNDETERMINED;
            if ($model->save()) {
                return $model->id;
            } else {
                return false;
            }
        }
    }

    /**
     * 更新记录
     * @param nunber $record_id 记录编号
     * @param nunber $status    记录状态
     * @param string $block_sid 区块链回执
     */
    public static function updateRecord($record_id, $status, $block_sid = null)
    {
        $model = self::findOne($record_id);
        $model->status = $status;
        if ($block_sid) {
            $model->block_sid = $block_sid;
        }
        if ($model->update()) {
            return $model->id;
        } else {
            return false;
        }
    }
}
